package com.xxx.mch.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.mch.domain.dto.TaskScoreTemplateDto;
import com.xxx.mch.domain.po.TaskScore;
import com.xxx.mch.domain.request.CreateScoreRequest;
import com.xxx.mch.domain.request.QueryScoreRequest;
import com.xxx.mch.domain.request.RuleSearchRequest;
import com.xxx.mch.domain.request.TaskScoreTemplateRequest;
import com.xxx.mch.service.TaskScoreService;
import com.xxx.mch.utils.WrapMapper;
import com.xxx.mch.utils.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 任务得分
 *
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020-02-25
 */
@RestController
@RequestMapping ("taskScores")
@Api(tags = "任务分/完成分接口")
public class TaskScoreController
{

    @Autowired
    private TaskScoreService taskScoreService;

    @GetMapping (produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<PageInfo<TaskScore>> getList(@ModelAttribute @Validated QueryScoreRequest queryScoreRequest)
    {
        PageInfo<TaskScore> pageData = taskScoreService.queryScoreRecord(queryScoreRequest);
        return WrapMapper.ok(pageData);
    }


    @PostMapping (consumes = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> create(@RequestBody @Validated CreateScoreRequest createScoreRequest)
    {
        taskScoreService.createScoreRecord(createScoreRequest);
        return WrapMapper.ok();
    }


    @GetMapping (value = "type", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<TaskScore>> taskTypeFilter(@ModelAttribute @Validated QueryScoreRequest queryScoreRequest)
    {
        List<TaskScore> scores = taskScoreService.queryTaskTypes(queryScoreRequest);
        return WrapMapper.ok(scores);
    }


    @GetMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<TaskScoreTemplateDto>> taskTemplate(@RequestParam (value = "parentId", required = false) List<Integer> parentId,
                                                            @RequestParam (value = "depth", defaultValue = "4") Integer depth)
    {
        return WrapMapper.ok(taskScoreService.getTaskTemplate(parentId, depth, true, null));
    }


    @ApiOperation (value = "完成分细则搜索")
    @GetMapping (value = "rule/search", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<TaskScoreTemplateDto>> ruleSearch(@ModelAttribute @Validated RuleSearchRequest ruleSearchRequest)
    {
        return WrapMapper.ok(taskScoreService.ruleSearch(ruleSearchRequest));
    }


    @ApiOperation (value = "定量附加项细则查询,type参数为4")
    @GetMapping (value = "rule/{type}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<TaskScoreTemplateDto>> ruleQuery(@PathVariable("type") Integer type)
    {
        return WrapMapper.ok(taskScoreService.ruleQuery(type));
    }

    @PostMapping (value = "template", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> createOrUpdate(@RequestBody List<TaskScoreTemplateRequest> templateRequest)
    {
        taskScoreService.createOrUpdateTemplate(templateRequest);
        return WrapMapper.ok();
    }


    @DeleteMapping (value = "template/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> deleteTemplate(@PathVariable ("id") Integer id)
    {
        taskScoreService.deleteTemplate(id);
        return WrapMapper.ok();
    }
}
